<?php

namespace app\api\controller;

use app\api\model\LabelsCategory;
use think\exception\DbException;
use think\response\Json;

class Rent extends Common
{
    public function category(): Json
    {
        $page = $this->params['page'] ?? 1;
        $where = ['parent_id' => 0, 'is_hidden' => 0];

        try {
            $data = Labels::all(function ($query) use ($where, $page) {
                $query->field('id, name, icon, description')
                    ->where($where)->order('sort desc, id asc')->page($page, 15);
            });
        } catch (DbException $e) {
            return $this->miss($e->getMessage());
        }

        foreach ($data as $key => $item) {
            $data[$key]['icon'] = changeUrl($item['icon']);
        }

        return $this->pass($data);
    }
}
